package dtd.PHS.VND_ExchangeRate;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import dtd.PHS.VND_ExchangeRate.data.MyDate;
import dtd.PHS.VND_ExchangeRate.data.VNExDatabase;
import dtd.PHS.VND_ExchangeRate.ui.CalculatorView;
import dtd.PHS.VND_ExchangeRate.ui.adapters.CurrencyAmountItem;
import dtd.PHS.VND_ExchangeRate.ui.adapters.PreviewSMSAdapter;
import dtd.PHS.VND_ExchangeRate.utils.Helpers;

public class PreviewSMS extends VNExActivity {

	private ListView listview;
	private Button btShareSMS;
	private PreviewSMSAdapter adapter;
	private Button btShareEmail;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.preview_sms);
		createViews();
	}

	private void createViews() {
		createListView();		
		createButtonShare();
	}

	private void createListView() {
		Intent src = getIntent();		
		Bundle bundle = src.getBundleExtra(CalculatorView.CURRENCIES_INFO);
		adapter = new PreviewSMSAdapter(bundle);
		listview = (ListView) findViewById(R.id.listview);
		listview.setAdapter(adapter);
	}

	private void createButtonShare() {
		btShareSMS = (Button) findViewById(R.id.btSharePerSMS);
		btShareSMS.setOnClickListener(new MyClickListener() {
			@Override
			public void broadcastResult(String content) {
				Helpers.broadcastSMSIntent(content);
			}
		});
		
		btShareEmail = (Button) findViewById(R.id.btSharePerEmail);
		btShareEmail.setOnClickListener(new MyClickListener() {
			@Override
			public void broadcastResult(String content) {
				Helpers.broadcastEmailIntent(PreviewSMS.this, content);
			}
		});
	}


	private String createContent(
			ArrayList<CurrencyAmountItem> selectedItems, MyDate date) {
		String content = getString(R.string.Calculate_SMSTitle) + " " + date.toString()+ "\n  ";
		boolean passedFirstChecked= false;
		for(int i = 0; i < selectedItems.size() ; i++) {
			if (passedFirstChecked) content +=" = "; else content+= "  ";
			CurrencyAmountItem item = selectedItems.get(i);
			content += item.getCurrency() + " " + item.getAmount() + "\n";
			passedFirstChecked = true;
		}
		content += "\n"+ getString(R.string.SMSBottomLine);
		return content;
	}
	
	private void saveLastSelectedCurrencies(
			ArrayList<CurrencyAmountItem> selectedItems) {
		String str = "";
		for(int i = 0 ; i  < selectedItems.size() ; i++) {
			str += selectedItems.get(i).getCurrency();
			if ( i != selectedItems.size() - 1 ) 
				str += ",";				
		}
		Helpers.setPreference(Helpers.PREF_LAST_CHOOSEN_PREVIEW_SMS, str);
	}
	
	abstract class MyClickListener implements View.OnClickListener {

		@Override
		public void onClick(View v) {
			ArrayList<CurrencyAmountItem> selectedItems = adapter.getSelectedItems();
			MyDate date = VNExDatabase.getDate();
			String content = createContent(selectedItems, date);
			saveLastSelectedCurrencies( selectedItems );
			broadcastResult(content);		
		}

		public abstract void broadcastResult(String content);
		
	}
}
